<?php

/**
 * Controller_research
 */

class Controller_research extends MyApp {

	public function __construct()
	{

		parent::__construct();

		$this->model->subject 		= YOU::loadModel('subject');
		$this->model->grade   		= YOU::loadModel('grade');
		$this->model->appoint    	= YOU::loadModel('appoint');
		$this->model->staff    		= YOU::loadModel('staff');
	}

	/** 首页 **/
	public function actionIndex()
	{

		$this->tpl->display();
	}
	/** 教研组列表 **/
	public function actionresearchList()
	{
		// 所有老师
		$staff_lists = $this->model->staff->getAll();
		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_research`");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$research_lists = $this->db->fetchAll("SELECT * FROM `tbl_research` ORDER BY research_id DESC LIMIT {$start}, ".PAGE_LIMIT."");
		foreach ($research_lists as $key => $value){
			// 普通员工
			$row  = $this->model->appoint->getResearch($value['research_id']);

			foreach ($row as $k => $v){
				// 员工姓名
				$rows  = $this->model->staff->getinfo($v['staff_id']);
				$research_lists[$key]['staff_name'][$v['staff_id']] = $rows['staff_name'];
			}
		}
		// 添加成员
		if ($_POST) {
			$research_id     		= $this->post->research_id;
			$staff_id     			= $this->post->staff_id;

			foreach ($staff_id as $key => $value){

				// 查找任职表
				$appoint_data =$this->model->appoint->getResRow($value,$research_id);
				if (!empty($appoint_data)) {

					// 写入数据库
					$info = array(

					"research_id"      		=> $research_id,
					"staff_id"      		=> $value,
					"appoint_common"      	=> 1,
					"create_time"           => date("Y-m-d H:i:s"),

					);
					$this->db->do_update("tbl_appoint", $info, "appoint_id='{$appoint_id}'");
				}else {

					// 写入数据库
					$info = array(

					"research_id"      		=> $research_id,
					"staff_id"      		=> $value,
					"appoint_common"      	=> 1,
					"status"                => 1,
					"create_time"           => date("Y-m-d H:i:s"),
					"update_time"           => "0000-00-00 00:00:00",

					);

					$this->db->do_insert("tbl_appoint", $info);
				}
			}
			Helper_Client::location("添加成员成功", "index.php?a=research.researchList");
		}

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";
		$this->tpl->assign("page", $page->show(4, "yahoo"));

		$this->tpl->assign("research_lists", $research_lists);
		$this->tpl->assign("staff_lists", $staff_lists);
		$this->tpl->display('research/research.list.tpl.php');
	}
	/** 教研组添加 **/
	public function actionresearchAdd()
	{
		if ($_POST){

			$research_name     	= $this->post->research_name;

			// 写入数据库
			$info = array(

			"research_name"      	=> $research_name,

			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);
			$this->db->do_insert("tbl_research", $info);

			// 跳转
			Helper_Client::location("添加成功", "index.php?a=research.researchlist");
		}


		$this->tpl->display('research/research.add.tpl.php');
	}
	/** 教研组修改 **/
	public function actionresearchUpdate()
	{
		$research_id   = (int)$this->get->research_id;
		$research_data = $this->db->fetchROW("SELECT * FROM `tbl_research` WHERE `research_id`='{$research_id}'");

		if (empty($research_data)){
			Helper_Client::alert("年级不存在");
		}

		if ($_POST){

			$research_name     	    = $this->post->research_name;

			// 写入数据库
			$info = array(

			"research_name"      	=> $research_name,
			"create_time"           => date("Y-m-d H:i:s"),
			);
			$this->db->do_update("tbl_research", $info, "research_id='{$research_id}'");

			// 跳转
			Helper_Client::location("	修改成功", "index.php?a=research.researchList");
		}

		$this->tpl->assign("research_data", $research_data);
		$this->tpl->display('research/research.update.tpl.php');
	}
	/** 教研组删除 **/
	public function actionresearchDel()
	{
		$research_id = $this->get->research_id;
		$this->db->query("DELETE  FROM `tbl_research` WHERE research_id='{$research_id}'");

		// 跳转
		Helper_Client::location("删除成功", "index.php?a=research.researchList");
	}
	/** 教研组批量生成 **/
	public function actionresearchbatch()
	{
		$grade_list      = $this->db->fetchAll("SELECT `grade_name`,`grade_id` FROM `tbl_grade`");
		$subject_list    = $this->db->fetchAll("SELECT `subject_name`,`subject_id` FROM `tbl_subject`");

		if ($_POST){

			$grade_id     	  = $this->post->grade_id;
			$subject_id      = $this->post->subject_id;
			$research_name   = $this->post->research_name;

			$subject_name    = $this->model->subject->getNameById($subject_id);
			$grade_name      = $this->model->grade->getNameById($grade_id);
			// 年级所有班级
			$grade_lists      = $this->model->grade->getAll();
			$research_name = $grade_name.$subject_name.'教研组';

			// 批量添加
			if($grade_id == 0){

				foreach ($grade_lists as $k => $l){
					$grade_name= $l['grade_name'];

					$info = array(

					"research_name"      	=> $grade_name.$research_name,
					"status"                => 1,
					"create_time"           => date("Y-m-d H:i:s"),
					"update_time"           => "0000-00-00 00:00:00",

					);

					$this->db->do_insert("tbl_research", $info);
				}

			}else{
				//  单条添加
				$info = array(
				"grade_id"      	    => $grade_id,
				"subject_id"      	    => $subject_id,
				"research_name"      	=> $research_name,
				"status"                => 1,
				"create_time"           => date("Y-m-d H:i:s"),
				"update_time"           => "0000-00-00 00:00:00",

				);

				$this->db->do_insert("tbl_research", $info);
			}
			// 跳转
			Helper_Client::location("添加成功", "index.php?a=research.researchlist");
		}
		$this->tpl->assign("grade_list", $grade_list);
		$this->tpl->assign("subject_list", $subject_list);
		$this->tpl->display('research/research.batch.tpl.php');
	}

}

?>